﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Text;
using System.Xml;
using System.Data;

namespace DynamicRSS_CSharp
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String sqlCon = System.Configuration.ConfigurationManager.AppSettings.Get("connStr");
            String spName = System.Configuration.ConfigurationManager.AppSettings.Get("sp_name");

            // xoa tat ca cac output truoc do
            Response.Clear();
            Response.ContentType = "text/xml";

            //khai bao xml
            XmlTextWriter xml = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
            xml.WriteStartDocument();

            //rss tag
            xml.WriteStartElement("rss");
            xml.WriteAttributeString("version","2.0");

            //chi tiet rss

            xml.WriteStartElement("channel");
            xml.WriteElementString("title","Test RSS Feed");
             xml.WriteElementString("link", "http://testLink");
            xml.WriteElementString("description", "This is example Feed from SQL Server");
            xml.WriteElementString("copyright", "Copyright 2008. All rights reserved.");
            try
            {
                SqlDataAdapter reader;
                SqlConnection cn = new SqlConnection(sqlCon);
                SqlCommand cmd = new SqlCommand(spName, cn);
                cmd.CommandType = CommandType.StoredProcedure;

                cn.Open();
                reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    xml.WriteStartElement("item");
                    xml.WriteElementString("title", reader[1].ToString);
                    xml.WriteElementString("description", reader[2].ToString());
                    xml.WriteElementString("link", reader[3].ToString());
                    xml.WriteElementString("pubDate", reader[4].ToString());
                    xml.WriteEndElement();
                }
                reader.Close();
                cn.Close();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
            xml.WriteEndElement();
            xml.WriteEndElement();
            xml.WriteEndDocument();
            xml.Flush();
            xml.Close();
            Response.End();
        }
    }
}